#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->treeWidget->setHeaderLabel("动物");

    QTreeWidgetItem *item1 = new QTreeWidgetItem();
    item1->setText(0,"鱼");
    ui->treeWidget->addTopLevelItem(item1);

    QTreeWidgetItem *item2 = new QTreeWidgetItem();
    item2->setText(0,"摸鱼");
    item1->addChild(item2);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    const QString& text = ui->lineEdit->text();
    QTreeWidgetItem *item = new QTreeWidgetItem();
    item->setText(0,text);
    ui->treeWidget->addTopLevelItem(item);
}

void Widget::on_pushButton_2_clicked()
{
    QTreeWidgetItem* curItem = ui->treeWidget->currentItem();
    const QString& text = ui->lineEdit->text();
    QTreeWidgetItem *item = new QTreeWidgetItem();
    item->setText(0,text);
    curItem->addChild(item);
}

void Widget::on_pushButton_3_clicked()
{
    QTreeWidgetItem* curItem = ui->treeWidget->currentItem();
    if(curItem == nullptr)return;
    QTreeWidgetItem* parentItem = curItem->parent();
    if(parentItem == nullptr)
    {
        int index = ui->treeWidget->indexOfTopLevelItem(curItem);
        ui->treeWidget->takeTopLevelItem(index);
    }
    else
    {
        parentItem->removeChild(curItem);
    }
}
